﻿#include "ObjectManager.h"

//IO层
#include "IO/FileReader.h"
#include "IO/FileWriter.h"
#include "IO/SerialPortManager.h"

//UI层
#include "UI/newUI/MemsAutoTestSoftware.h"
#include "UI/newUI/ParameterConsole.h"
#include "UI/newUI/SetMEMSDataDialog.h"
#include "UI/newUI/SetPositionModeDialog.h"
#include "UI/newUI/SetRateModeDialog.h"
#include "UI/newUI/SetScriptDialog.h"
#include "UI/newUI/SetScriptLoopDialog.h"
#include "UI/newUI/SetScriptWaitDialog.h"
#include "UI/newUI/SetThermostatDialog.h"
#include "UI/newUI/SetTurnTableAndThermostatDialog.h"

//功能层
#include "ACT/MEMSData/MEMSDataCodec.h"
#include "ACT/MEMSData/MEMSDataHandler.h"
#include "ACT/MEMSData/MEMSDataSaver.h"
#include "ACT/Script/ScriptInstruction/ScriptInstruction.h"
#include "ACT/Script/ScriptRunner.h"
#include "ACT/Script/ScriptSaveLoader.h"
#include "ACT/Script/ScriptInstructionFactory.h"
#include "ACT/TurnTable/TurnTableCodec.h"
#include "ACT/TurnTable/TurnTableConnectionStateManager.h"
#include "ACT/TurnTable/TurnTableRunningStateManager.h"
#include "ACT/ConfigSaveLoader.h"

//ObjectManager::ObjectManager(QObject* parent):QObject(parent)
//{
//	initModules();
//	moveToThread();
//	objectInjection();
//	configSaveLoader->slot_loadConfig();
//	memsAutoTestSoftware->show();
//}
//ObjectManager::~ObjectManager()
//{
//	configSaveLoader->slot_saveConfig();
//	memsAutoTestSoftware->deleteLater();
//	for (QThread* pointer : threadPointerList) 
//	{//析构线程内的对象和线程本身，如果两者存在
//		pointer->quit();
//	}
//	if (qThreadPool->waitForDone())
//	{
//		qThreadPool->deleteLater();
//	}
//}
//void ObjectManager::initModules()
//{
//	DataQueue<int> a;
//
//	memsAutoTestSoftware = new MemsAutoTestSoftware();
//
//	//初始化各个串口
//	serialPortManager_turnTable = new SerialPortManager();//转台串口管理器
//	serialPortManager_turnTable->setObjectName("serialPortManager_turnTable");
//	serialPortManager_mEMSData = new SerialPortManager();//数据采集器串口管理器
//	serialPortManager_mEMSData->setObjectName("serialPortManager_mEMSData");
//	//初始化各UI
//	parameterConsole = new ParameterConsole(memsAutoTestSoftware);
//	parameterConsole->setObjectName("parameterConsole");
//	setMEMSDataDialog = new SetMEMSDataDialog(memsAutoTestSoftware);
//	setMEMSDataDialog->setObjectName("setMEMSDataDialog");
//	setTurnTableAndThermostatDialog = new SetTurnTableAndThermostatDialog(memsAutoTestSoftware);
//	setTurnTableAndThermostatDialog->setObjectName("setTurnTableAndThermostatDialog");
//	setScriptDialog = new SetScriptDialog(memsAutoTestSoftware);
//	setScriptDialog->setObjectName("setScriptDialog");
//	setPositionModeDialog = new SetPositionModeDialog(setScriptDialog);
//	setPositionModeDialog->setObjectName("setPositionModeDialog");
//	setRateModeDialog = new SetRateModeDialog(setScriptDialog);
//	setRateModeDialog->setObjectName("setRateModeDialog");
//	setScriptLoopDialog = new SetScriptLoopDialog(setScriptDialog);
//	setScriptLoopDialog->setObjectName("setScriptLoopDialog");
//	setScriptWaitDialog = new SetScriptWaitDialog(setScriptDialog);
//	setScriptWaitDialog->setObjectName("setScriptWaitDialog");
//	setThermostatDialog = new SetThermostatDialog(setScriptDialog);
//	setThermostatDialog->setObjectName("setThermostatDialog");
//	//初始化各核心组件
//	mEMSDataCodec = new MEMSDataCodec(memsAutoTestSoftware);//数据采集器编码解码器
//	mEMSDataCodec->setObjectName("mEMSDataCodec");
//	mEMSDataHandler = new MEMSDataHandler(memsAutoTestSoftware);
//	mEMSDataHandler->setObjectName("mEMSDataHandler");
//	mEMSDataSaver = new MEMSDataSaver(memsAutoTestSoftware);
//	mEMSDataSaver->setObjectName("mEMSDataSaver");
//	scriptRunner = new ScriptRunner(memsAutoTestSoftware);
//	scriptRunner->setObjectName("scriptRunner");
//	scriptSaveLoader = new ScriptSaveLoader(memsAutoTestSoftware);
//	scriptSaveLoader->setObjectName("scriptSaveLoader");
//	turnTableCodec = new TurnTableCodec(memsAutoTestSoftware);//转台编码解码器
//	turnTableCodec->setObjectName("turnTableCodec");
//	turnTableConnectionStateManager = new TurnTableConnectionStateManager(memsAutoTestSoftware);
//	turnTableConnectionStateManager->setObjectName("turnTableConnectionStateManager");
//	turnTableRunningStateManager = new TurnTableRunningStateManager(memsAutoTestSoftware);
//	turnTableRunningStateManager->setObjectName("turnTableRunningStateManager");
//	configSaveLoader = new ConfigSaveLoader(memsAutoTestSoftware);
//	configSaveLoader->setObjectName("configSaveLoader");
//	logSaver = new LogSaver(memsAutoTestSoftware);
//	logSaver->setObjectName("logSaver");
//
//	//初始化各个线程
//	qThread_serialPortManager_turnTable = new QThread();//转台串口管理器线程
//	addThreadToList(qThread_serialPortManager_turnTable);
//	qThread_serialPortManager_dataReader = new QThread();//数据采集器的串口管理器线程
//	addThreadToList(qThread_serialPortManager_dataReader);
//	qThreadPool = new QThreadPool();
//}
//void ObjectManager::objectInjection()
//{
//	//对象注入
//	fileReader->setQThreadPool(qThreadPool);
//	fileWriter->setQThreadPool(qThreadPool);
//	serialPortManager_turnTable->setLogSaver(logSaver);
//	serialPortManager_mEMSData->setLogSaver(logSaver);
//
//	memsAutoTestSoftware->setTurnTableRunningStateManager(turnTableRunningStateManager);
//	memsAutoTestSoftware->setTurnTableCodec(turnTableCodec);
//	memsAutoTestSoftware->setMEMSDataCodec(mEMSDataCodec);
//	memsAutoTestSoftware->setMEMSDataHandler(mEMSDataHandler);
//	memsAutoTestSoftware->setMEMSDataSaver(mEMSDataSaver);
//	memsAutoTestSoftware->setScriptRunner(scriptRunner);
//	memsAutoTestSoftware->setScriptSaveLoader(scriptSaveLoader);
//	memsAutoTestSoftware->setLogSaver(logSaver);
//	memsAutoTestSoftware->setSerialPortManager_turnTable(serialPortManager_turnTable);
//	memsAutoTestSoftware->setSerialPortManager_mEMSData(serialPortManager_mEMSData);
//	memsAutoTestSoftware->setSetTurnTableAndThermostatDialog(setTurnTableAndThermostatDialog);
//	memsAutoTestSoftware->setSetMEMSDataDialog(setMEMSDataDialog);
//	memsAutoTestSoftware->setParameterConsole(parameterConsole);
//	memsAutoTestSoftware->setSetScriptDialog(setScriptDialog);
//
//	setMEMSDataDialog->setMEMSDataHandler(mEMSDataHandler);
//	setMEMSDataDialog->setMEMSDataCodec(mEMSDataCodec);
//	setMEMSDataDialog->setMEMSDataSaver(mEMSDataSaver);
//	setMEMSDataDialog->setSerialPortManager(serialPortManager_mEMSData);
//
//	setScriptDialog->setSetScriptWaitDialog(setScriptWaitDialog);
//	setScriptDialog->setSetScriptLoopDialog(setScriptLoopDialog);
//	setScriptDialog->setSetThermostatDialog(setThermostatDialog);
//	setScriptDialog->setSetPositionModeDialog(setPositionModeDialog);
//	setScriptDialog->setSetRateModeDialog(setRateModeDialog);
//	setScriptDialog->setScriptRunner(scriptRunner);
//
//	setTurnTableAndThermostatDialog->setTurnTableCodec(turnTableCodec);
//	setTurnTableAndThermostatDialog->setTurnTableRunningStateManager(turnTableRunningStateManager);
//	setTurnTableAndThermostatDialog->setSerialPortManager(serialPortManager_turnTable);
//
//	mEMSDataCodec->setSerialPortManager(serialPortManager_mEMSData);
//	mEMSDataCodec->setLogSaver(logSaver);
//
//	mEMSDataHandler->setMEMSDataCodec(mEMSDataCodec);
//	mEMSDataHandler->setLogSaver(logSaver);
//
//	mEMSDataSaver->setMEMSDataHandler(mEMSDataHandler);
//	mEMSDataSaver->setFileWriter(fileWriter);
//	mEMSDataSaver->setLogSaver(logSaver);
//
//	ScriptInstruction::setMEMSDataHandler(mEMSDataHandler);
//	ScriptInstruction::setScriptRunner(scriptRunner);
//	ScriptInstruction::setTurnTableCodec(turnTableCodec);
//	ScriptInstruction::setTurnTableRunningStateManager(turnTableRunningStateManager);
//	ScriptInstruction::setTurnTableConnectionStateManager(turnTableConnectionStateManager);
//
//	scriptRunner->setTurnTableCodec(turnTableCodec);
//	scriptRunner->setLogSaver(logSaver);
//
//	scriptSaveLoader->setFileWriter(fileWriter);
//	scriptSaveLoader->setFileReader(fileReader);
//	scriptSaveLoader->setScriptRunner(scriptRunner);
//	scriptSaveLoader->setLogSaver(logSaver);
//
//	turnTableCodec->setSerialPortManager(serialPortManager_turnTable);
//	turnTableCodec->setLogSaver(logSaver);
//
//	turnTableConnectionStateManager->setTurnTableCodec(turnTableCodec);
//	turnTableConnectionStateManager->setLogSaver(logSaver);
//
//	turnTableRunningStateManager->setTurnTableCodec(turnTableCodec);
//
//	configSaveLoader->setMemsAutoTestSoftware(memsAutoTestSoftware);
//	configSaveLoader->setParameterConsole(parameterConsole);
//	configSaveLoader->setSetPositionModeDialog(setPositionModeDialog);
//	configSaveLoader->setSetRateModeDialog(setRateModeDialog);
//	configSaveLoader->setSetTurnTableAndThermostatDialog(setTurnTableAndThermostatDialog);
//	configSaveLoader->setMEMSDataCodec(mEMSDataCodec);
//	configSaveLoader->setMEMSDataHandler(mEMSDataHandler);
//	configSaveLoader->setMEMSDataSaver(mEMSDataSaver);
//	configSaveLoader->setScriptRunner(scriptRunner);
//	configSaveLoader->setTurnTableCodec(turnTableCodec);
//	configSaveLoader->setLogSaver(logSaver);
//	configSaveLoader->setSerialPortManager_turnTable(serialPortManager_turnTable);
//	configSaveLoader->setSerialPortManager_mEMSData(serialPortManager_mEMSData);
//	configSaveLoader->setFileReader(fileReader);
//	configSaveLoader->setFileWriter(fileWriter);
//
//	logSaver->setFileWriter(fileWriter);
//}
//void ObjectManager::moveToThread()
//{
//	moveObjectToThread(serialPortManager_turnTable, qThread_serialPortManager_turnTable);//将转台串口管理器移动到对应线程
//	moveObjectToThread(serialPortManager_mEMSData, qThread_serialPortManager_dataReader);//将数据采集器串口管理器移动到对应线程
//}
//void ObjectManager::moveObjectToThread(QObject* object, QThread* thread)
//{
//	object->moveToThread(thread);
//	connect(thread, &QThread::finished, thread, &QObject::deleteLater);
//	connect(thread, &QThread::finished, object, &QObject::deleteLater);
//	thread->start();
//}
//void ObjectManager::addThreadToList(QThread* thread)
//{
//	threadPointerList.append(thread);
//}

#include "ObjectManager.h"

//config file
#include "MacroDefinition.h"

#include "qdebug.h"
ObjectManager* ObjectManager::objectManager = Q_NULLPTR;

ObjectManager::ObjectManager(QObject *parent) : QObject(parent)
{
	objectManager = this;
	objectManager->setObjectName("objectManager");
	objectCreation();
	objectInjection();
	objectInitialization();
	logSaver->saveLog("ObjectManager created");
	getObjFromMap<MemsAutoTestSoftware>()->show();
}

ObjectManager::~ObjectManager()
{
	for (QString objName : objMap.keys())
	{
		removeFromObjMap(objName);
	}
}

ObjectManager* ObjectManager::getObjectManager()
{
	return Q_NULLPTR == objectManager ? objectManager = new ObjectManager() : objectManager;
}

void ObjectManager::objectCreation()
{
	logSaver->saveLog("-------------------------objectCreation-------------------------");
	//IO
	addToObjMap<FileReader>();
	addToObjMap<FileWriter>();
	addToObjMap<SerialPortManager>("serialPortManager_turnTable");
	addToObjMap<SerialPortManager>("serialPortManager_mEMSData");
	//ACT
	addToObjMap<MEMSDataCodec>();
	addToObjMap<MEMSDataHandler>();
	addToObjMap<MEMSDataSaver>();
	addToObjMap<ScriptRunner>();
	addToObjMap<ScriptSaveLoader>();
	addToObjMap<TurnTableCodec>();
	addToObjMap<TurnTableConnectionStateManager>();
	addToObjMap<TurnTableRunningStateManager>();
	addToObjMap<ConfigSaveLoader>();
	//UI
	addToObjMap<MemsAutoTestSoftware>();
	addToObjMap<ParameterConsole>();
	addToObjMap<SetMEMSDataDialog>();
	addToObjMap<SetTurnTableAndThermostatDialog>();
	addToObjMap<SetScriptDialog>();
	addToObjMap<SetPositionModeDialog>();
	addToObjMap<SetRateModeDialog>();
	addToObjMap<SetScriptLoopDialog>();
	addToObjMap<SetScriptWaitDialog>();
	addToObjMap<SetThermostatDialog>();

	logSaver->saveLog("-------------------------objectCreation-------------------------");
}

#ifdef OBJECTINJECTION_ON
void ObjectManager::objectInjection()
{
	logSaver->saveLog("-------------------------objectInjection-------------------------");
	for (QObject* obj : objMap.values())
	{
		ObjAutoInject* temp = dynamic_cast<ObjAutoInject*>(obj);
		if (temp)
		{
			temp->objectAutoInjection(&objMap);
		}
	}
	logSaver->saveLog("-------------------------objectInjection-------------------------");
}
#else
void ObjectManager::objectInjection()
{}
#endif

void ObjectManager::objectInitialization()
{
	logSaver->saveLog("-------------------------objectInitialization-------------------------");
	getObjFromMap<ConfigSaveLoader>()->loadConfig();//读取配置文件
	logSaver->saveLog("-------------------------objectInitialization-------------------------");
}

void ObjectManager::removeFromObjMap(QObject* obj)
{
	if (Q_NULLPTR == obj)
	{
		return;
	}
	removeFromObjMap(obj->objectName());
}

void ObjectManager::removeFromObjMap(QString objName)
{
	QString name = objName;
	if (name.isEmpty())
	{
		logSaver->saveLog(QString("removeFromObjMap , obj name is empty"));
		return;
	}
	if (!objMap.contains(name))
	{
		logSaver->saveLog(QString("removeFromObjMap , obj %1 is not in objMap").arg(name));
		return;
	}
	objMap.take(name)->deleteLater();
	logSaver->saveLog(QString("removeFromObjMap , obj %1 is removed from objMap").arg(name));
}

void ObjectManager::addToObjMap(QObject* obj, QString objName)
{
	if (Q_NULLPTR == obj)
	{
		logSaver->saveLog("addToObjMap , obj is null");
		return;
	}
	obj->setObjectName(objName);
	QString name = obj->objectName();
	if (name.isEmpty())
	{
		logSaver->saveLog(QString("addToObjMap , obj name is empty"));
		obj->deleteLater();
		return;
	}
	if (objMap.contains(name))
	{
		logSaver->saveLog(QString("addToObjMap , obj %1 is already in objMap").arg(name));

		obj->deleteLater();
		return;
	}
	objMap.insert(name, obj);
	logSaver->saveLog(QString("addToObjMap , obj %1 is added to objMap").arg(name));
}